<?php
App::uses('AppModel', 'Model');
/**
 * Login Model
 *
 * @property Usuario $Usuario
 * @property Usuario $Usuario
 */
class Login extends AppModel {

/**
 * Use table
 *
 * @var mixed False or table name
 */
	public $useTable = 'login';

/**
 * Display field
 *
 * @var string
 */
	public $displayField = 'login';

/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'login' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Você deve escolher um login',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			'unique' => array(
					'rule' => array('isUnique'),
					'message' => 'Esse login já existe em nosso servidor',
					//'allowEmpty' => false,
					//'required' => false,
					//'last' => false, // Stop validation after this rule
					//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'senha' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Você deve digitar uma senha',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			'passwordequal'=>array(
				'rule' =>'cmppasswords',
				'message' => 'Senhas não conferem!'
			)
		),
		'confirmacao_senha' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Você deve digitar uma senha',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'tipo' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				'message' => 'Escolha a categoria do usuário',
				//'allowEmpty' => false,
				'required' => true,
				//'last' => false, // Stop validation after this rule
				'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'usuario_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
	public $belongsTo = array(
		'Usuario' => array(
			'className' => 'Usuario',
			'foreignKey' => 'usuario_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);
	
	public function beforeSave($options=array()){
		if(isset($this->data[$this->alias]['senha'])){
			$this->data[$this->alias]['senha'] = AuthComponent::password($this->data[$this->alias]['senha']);
		}
		return true;
	}
	
	function cmppasswords(){
		return strcmp($this->data[$this->alias]['senha'],$this->data[$this->alias]['confirmacao_senha']) == 0;
	}
	
	function idsDeRevisores(){
		return $this->find('list', array('conditions'=>array('tipo'=>REVISOR), 'fields'=>array('usuario_id')));
	}
	
	function chkPasswd($passwd = null){
		$hash = $this->find('first', array('fields' => 'senha', 'conditions' => array('Login.' . $this->primaryKey => AuthComponent::user('id')), 'recursive' => -1));
		return strcmp($hash['Login']['senha'], AuthComponent::password($passwd)) == 0;
	}
}
